package com.ggshily.swf.abc;

import java.io.DataInput;
import java.io.IOException;

/**
 * This entry provides a means of embedding arbitrary key/value pairs into the ABC file. The AVM2 will ignore all such entries.
 * 
 * @author playfish_chg
 *
 */
public class ABCMetadata implements IABCData
{

	/**
	 * U30, an index into the <code>string</code> array of the constant pool; it provides a name for the metadata entry.
	 * The value of the name field must not be zero.
	 */
	public int name;
	
	/**
	 * U30, denotes the number of items that follow in the <code>items</code> array.
	 */
	public int itemCount;
	
	/**
	 * items[item_count], this entry consists of item_count elements that are interpreted as key/value pairs of indices
	 * into the string table of the constant pool. If the value of key is zero,
	 * this is a keyless entry and only carries a value.
	 */
	public ItemInfo[] items;
	
	public ABCMetadata()
	{
		super();
	}

	public ABCMetadata(DataInput di) throws IOException
	{
		parseInput(di);
	}

	@Override
	public void parseInput(DataInput di) throws IOException
	{
		name = ABCUtil.readU30(di);
		assert name != 0 : "Name can't be 0 in metadata!!!";
		
		itemCount = ABCUtil.readU30(di);
		
		items = new ItemInfo[itemCount];
		for(int i = 0; i < itemCount; i++)
		{
			items[i] = new ItemInfo(di);
		}
	}

	@Override
	public void parseInput(DataInput di, ABCFile file)
	{
		// TODO Auto-generated method stub
		
	}

}


class ItemInfo
{
	public ItemInfo(DataInput di) throws IOException
	{
		key = ABCUtil.readU30(di);
		value = ABCUtil.readU30(di);
	}
	/**
	 * U30
	 */
	public int key;
	
	/**
	 * U30
	 */
	public int value;
}